#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/8/17 10:03:49
# @File           : views.py
# @IDE            : PyCharm
# @desc           : 派工视图
from typing import List

from fastapi import APIRouter, Depends, Path, Request

from core.dependencies import IdList
from core.crud import construct_filter
from utils.response import SuccessResponse
from . import schemas, crud, params, models
from ..taskpool.models import DCTask
from ...base.auth.validate import Auth, AllUserAuth
from sqlalchemy.orm import joinedload

app = APIRouter()

###################################################
#     对接基础数据，可派工人员列表                  #
###################################################
@app.get("/task/teams", summary="班组列表")
async def get_emp_teams(
        request: Request,
        p: params.TeamUserParams = Depends(),
        auth: Auth = Depends(AllUserAuth()),
):
    datas, count = await crud.TaskAssignUsersDal(auth.db).get_emp_groups(headers=request.headers, **p.dict())
    return SuccessResponse(datas, count=count)

@app.get("/task/users", summary="人员列表")
async def get_emp_users(
        request: Request,
        p: params.TeamUserParams = Depends(),
        auth: Auth = Depends(AllUserAuth()),
):
    datas, count = await crud.TaskAssignUsersDal(auth.db).get_emp_users(headers=request.headers, **p.dict())
    return SuccessResponse(datas, count=count)


###################################################
#     任务派工                                     #
###################################################
@app.get("/task/assignment", summary="派工结果")
async def get_assign_list(
        p: params.AssignUserParams = Depends(),
        auth: Auth = Depends(AllUserAuth())
):
    v_where = None
    if p.task:
        v_where = construct_filter(DCTask, **p.task)
    del p.task
    datas, count = await crud.TaskAssignUsersDal(auth.db).get_datas(
        **p.dict(),
        v_schema=schemas.TaskAssignUsersOut,
        v_return_count=True,
        v_join=[["task"]],
        v_options=[joinedload(models.DCTaskAssignUsers.task)],
        v_where=v_where
    )
    return SuccessResponse(datas, count=count)


@app.get("/task/{ticket_id}/assignment", summary="获取生产任务派工")
async def get_team(
        ticket_id: str,
        auth: Auth = Depends(AllUserAuth())
):
    datas, count = await crud.TaskWorkTeamDal(auth.db).get_datas(
        v_join=[["task"]],
        v_where=[DCTask.ticket_id == ticket_id],
        v_schema=schemas.TaskAssignUsersSimpleOut,
        v_return_count=True
    )
    return SuccessResponse(datas, count=count)


@app.post("/task/assignment", summary="派工和资源分配")
async def create_assign(
        datas: List[schemas.TaskAssignUsers],
        auth: Auth = Depends(AllUserAuth())
):
    for data in datas:
        # todo: 派工，推送工单信息至用户
        data.create_user = auth.user.user_str
    return SuccessResponse(await crud.TaskAssignUsersDal(auth.db).create_datas([data.model_dump() for data in datas]))


@app.delete("/task/assignment", summary="批量删除派工", description="软删除")
async def delete_feedback(
        ids: IdList = Depends(),
        auth: Auth = Depends(AllUserAuth())
):
    # todo: 撤回工单派工消息
    await crud.TaskAssignUsersDal(auth.db).delete_datas(ids=ids.ids, v_soft=True)
    return SuccessResponse("删除成功")
